package com.hzdl.controller;

import com.hzdl.bean.Agreement;
import com.hzdl.bean.AgreementDetail;
import com.hzdl.bean.Result;
import com.hzdl.common.Constants;
import com.hzdl.service.AgreementService;
import com.hzdl.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * @author: bxc
 * @date: 2018/9/5 0005
 * @description: 合同Controller
 */
@Controller
@RequestMapping("/agreen")
public class AgreementController {

    @Autowired
    private AgreementService agreementService;

    //根据项目ID获取该项目下的所有合同信息
    @RequestMapping("/getAll")
    public String getAllAgreement(long prid, Model model){
        model.addAttribute("prid",prid);
        List<AgreementDetail> agreementDetails = agreementService.getAllAgreement(prid);
        model.addAttribute("agreens",agreementDetails);
        return "agreenManager";
    }

    //根据合同ID更新对应合同信息(终止合同)
    @RequestMapping("/stop")
    @ResponseBody
    public Object stop(Long aid){
        String nowTime = DateUtils.getNowTime();
        String state = Constants.YI_ZHONG_ZHI;
        agreementService.updateAgreement(aid,nowTime,state);
        Result result = new Result();
        result.setErrorCode(200);
        result.setErrorMsg("合同信息更新成功！");
        return result;
    }

    //去添加合同页面
    @RequestMapping("/toAdd")
    public String toAddAgreen(HttpServletRequest req){
        String pri = req.getParameter("prid");
        int prid = Integer.parseInt(pri);
        req.setAttribute("prid2",prid);
        return "addAgreen";
    }

    //添加新合同
    @RequestMapping(value = "/addAgreen",method = RequestMethod.POST)
    public String addAgreement(String start_time,String end_time,String pay_period,String is_New_Deal,String prepay,String price,
                               String free_period,String rate_increase,String person,String phone,int prid,Model model){
//        System.err.println(start_time+" , "+end_time+" , "+pay_period+" , "+is_New_Deal+" , "+prepay+" , "+price+" , "
//                +free_period+" , "+rate_increase+" , "+person+" , "+phone+" , "+prid);
        AgreementDetail agreementDetail = new AgreementDetail();
        agreementDetail.setStart_time(DateUtils.convert(start_time));
        agreementDetail.setEnd_time(DateUtils.convert(end_time));
        agreementDetail.setPay_period(Integer.parseInt(pay_period));
        agreementDetail.setIs_New_Deal(Integer.parseInt(is_New_Deal));
        agreementDetail.setPrepay(Double.parseDouble(prepay));
        agreementDetail.setPrice(Double.parseDouble(price));
        agreementDetail.setFree_period(Long.parseLong(free_period));
        agreementDetail.setRate_increase(Double.parseDouble(rate_increase));
        agreementDetail.setPerson(person);
        agreementDetail.setPhone(phone);
        agreementDetail.setPrid(prid);
        agreementDetail.setState(Constants.YOU_XIAO);
        agreementService.addAgreement(agreementDetail);
        model.addAttribute("addmsg","添加成功！");
        return "addAgreen";
    }

    //获取房间下的所有历史合同
    @RequestMapping("/history")
    public String getAllHistoryAgreen(int rid,Model model){
        List<Agreement> historyAgreens = agreementService.getAllHistoryAgreen(rid);
        model.addAttribute("history",historyAgreens);
        for(Agreement agreement:historyAgreens){
            double price = agreement.getAgreementDetail().getPrice();
            double rentsize =agreement.getAgreementDetail().getRent_size();
            Date endDate = agreement.getAgreementDetail().getEnd_time();
            Date beginDate = agreement.getAgreementDetail().getStart_time();
            long month=(endDate.getTime()-beginDate.getTime())/(30*24*60*60*1000);
            agreement.setValue(Math.abs((long) (month*price*rentsize)));
            long aid = agreement.getAgreementDetail().getAid();
            agreementService.updateAgreen(Math.abs((long) (month*price*rentsize)),aid);
        }
        return "historyAgreen";
    }


    //异常合同
    @RequestMapping("/getAbnormal")
    public String getArrearsAgm(Model model){
//        agreementService.updateIsNormal();//改为在数据库中用存储过程+触发器更新了
        List<AgreementDetail> abnormal=agreementService.getArrearsAgm();
//        System.err.println(abnormal);
        model.addAttribute("abnormal",abnormal);
        return "abnormalManager";
    }






}
